/*
 * This project provides a number of implementations for indexing and evaluating
 * <i>Continuous Top-k Queries over Text Streams</i>
 * and has been developped as part of the work described in the article:
 * 
 * Vouzoukidou, N., Amann, B., & Christophides, V. (2012, October). 
 * Processing continuous text queries featuring non-homogeneous scoring functions.
 * In Proceedings of the 21st ACM international conference on Information and 
 * knowledge management (pp. 1065-1074). ACM.
 * http://dl.acm.org/citation.cfm?id=2398404
 * 
 * This software is distributed under the GNU General Public License and as an
 * additional condition of usage, please cite the aforementioned article.
 * 
 * Copyright (C) 2012 ICS/FORTH, Greece and 
 *               2012 LIP6, France
 * 
 * Author: Vouzoukidou Nelly, email: nelly.vouzoukidou (at) gmail (dot) com
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package gr.forth.ics.continuoustopk.datasets;

import gr.forth.ics.continuoustopk.Constants;
import gr.forth.ics.continuoustopk.Query;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Iterator;
import java.util.Scanner;

/**
 *
 * @author Vouzoukidou Nelly, email: vuzukid (at) gmail (dot) com
 */
public abstract class QueriesIterator implements Iterator<Query> {
    private QueriesIterator() {}

    public void remove() {
        throw new UnsupportedOperationException();
    }

    public static QueriesIterator queriesWithLength(int length) throws FileNotFoundException {
        return queriesFromFile(DatasetConstants.queriesWithLength[length]);
    }

    public static QueriesIterator mixedQueries() throws FileNotFoundException {
        return queriesFromFile(DatasetConstants.queriesFile);
    }

    private static QueriesIterator queriesFromFile(final File file) throws FileNotFoundException {
        return new QueriesIterator() {
            Scanner in = new Scanner(file);

            public boolean hasNext() {
                return in != null;
            }

            public Query next() {
                if (!hasNext()) throw new IllegalStateException();

                String line = in.nextLine();
                String[] columns = line.split(";");
                String[] terms = columns[0].split(" ");
                String[] weightsStr = columns[1].split(" ");
                double[] weights = new double[weightsStr.length];
                for (int i = 0; i < weights.length; i++) {
                   weights[i] = Double.parseDouble(weightsStr[i]);
                }
                if (!in.hasNextLine()) {
                    in = null;
                }
                return Query.newTopOne(terms, weights, Constants.queryScoreStrategy.score());//newSortedTopK(terms, weights, Constants.k);
            }
        };
    };

    /**
     * An infinit set of generated queries
     * @return a QueriesIterator of random Query objects.
     */
    public static QueriesIterator generatedQueries() {
        return new QueriesIterator() {
            public boolean hasNext() {
                return true;
            }

            public Query next() {
                int querySize = Constants.random.nextInt(
                        Constants.maxQuerySize + 1 - DatasetConstants.minQuerySize) + DatasetConstants.minQuerySize;
                String[] terms = Datasets.terms(querySize, DatasetConstants.vocabularySize);
                double[] weights = Datasets.getWeights(querySize);
                Query query = Query.newTopOne(terms, weights, Constants.queryScoreStrategy.score());
                return query;
            }
        };
    }
    
}
